package com.jincp.image.pick.adapter.file;


import android.text.TextUtils;
import android.text.format.Formatter;
import android.widget.CompoundButton;

import com.jcp.baselibrary.brvah.BaseQuickAdapter;
import com.jcp.baselibrary.brvah.BaseViewHolder;
import com.jincp.image.R;
import com.jincp.image.pick.DocumentPicker;
import com.jincp.image.pick.bean.Document;

import java.util.List;


/**
 * Created by droidNinja on 29/07/16.
 */
public class DocumentAdapter extends BaseQuickAdapter<Document, BaseViewHolder> {

    private DocumentPicker documentPicker;

    public DocumentAdapter(List<Document> items, List<String> selectedPaths) {
        super(R.layout.item_doc_layout, items);
        documentPicker = DocumentPicker.getInstance();
        formatSelect(selectedPaths);
    }

    private void formatSelect(List<String> selectedPaths) {
        if (selectedPaths != null && !selectedPaths.isEmpty()) {
            for (Document document : mData) {
                for (String selectedPath : selectedPaths) {
                    if (TextUtils.equals(document.path, selectedPath)) {
                        document.isSelected = true;
                        break;
                    }
                }
            }
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, final Document item) {
        helper.setImageResource(R.id.file_iv, item.getTypeDrawable())
                .setText(R.id.file_name_tv, item.getTitle())
                .setText(R.id.file_size_tv, Formatter.formatShortFileSize(mContext, item.size))
                .setChecked(R.id.checkbox, item.isSelected);

        helper.setOnCheckedChangeListener(R.id.checkbox, new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton checkBox, boolean isChecked) {
                item.isSelected = isChecked;
                documentPicker.addSelectedDocumentItem(isChecked, item);
            }
        });
    }
}
